using System;
using System.IO;
using System.Threading.Tasks;
using SQLite;

namespace DesktopWidgetApp.Core.Helpers
{
    /// <summary>
    /// 数据库帮助类
    /// 提供数据库连接管理和初始化功能
    /// </summary>
    public static class DatabaseHelper
    {
        /// <summary>
        /// 获取数据库路径
        /// </summary>
        /// <returns>数据库文件路径</returns>
        public static string GetDatabasePath()
        {
            var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            var appFolder = Path.Combine(appDataPath, "DesktopWidgetApp");
            
            // 确保应用文件夹存在
            if (!Directory.Exists(appFolder))
            {
                Directory.CreateDirectory(appFolder);
            }
            
            // 返回数据库文件路径
            return Path.Combine(appFolder, "status_database.db");
        }
        
        /// <summary>
        /// 初始化数据库
        /// </summary>
        /// <returns>异步任务</returns>
        public static async Task InitializeDatabaseAsync()
        {
            var databasePath = GetDatabasePath();
            
            // 创建数据库连接
            using (var connection = new SQLiteAsyncConnection(databasePath))
            {
                // 初始化必要的表
                await connection.CreateTableAsync<Shared.Status>();
                
                // 可以在这里添加其他初始化逻辑，如创建索引、种子数据等
            }
        }
        
        /// <summary>
        /// 获取数据库连接
        /// </summary>
        /// <returns>SQLite异步连接</returns>
        public static SQLiteAsyncConnection GetConnection()
        {
            var databasePath = GetDatabasePath();
            return new SQLiteAsyncConnection(databasePath);
        }
    }
}